﻿package net.wangit.framework.license;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.wangit.util.Checker;
import net.wangit.util.XMLUtil;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


/**
 * license授权
 * 
 * @author watson Feb 14, 2010
 */
public final class L {

	private static SimpleDateFormat formatter = new SimpleDateFormat(
			"yyyy-MM-dd");
	private String id;
	private String product;
	private String version;
	private String ip;
	private Date expiration;
	private String code;
	private String signature;

	public L(String id, String product, String version, String code) {
		this.id = id;
		this.product = product;
		this.version = version;
		this.code = code;
	}

	public String getProduct() {
		return product;
	}

	public String getVersion() {
		return version;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Date getExpiration() {
		return expiration;
	}

	public void setExpiration(Date expiration) {
		this.expiration = expiration;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public byte[] getFingerprint() {
		StringBuffer buf;
		buf = new StringBuffer(100);
		buf.append(id);
		buf.append(product);
		buf.append(version);
		buf.append(code);
		if (ip != null)
			buf.append(ip);
		if (expiration != null)
			buf.append(formatter.format(expiration));
		try {
			return buf.toString().getBytes("utf-8");
		} catch (Exception e) {
			return buf.toString().getBytes();
		}
	}

	public static Document toXML(L l) throws Exception {
		Document doc = DocumentHelper.createDocument();
		Element el = doc.addElement("license");
		el.addElement("id").setText(l.getId());
		el.addElement("product").setText(l.getProduct());
		el.addElement("version").setText(l.getVersion());
		el.addElement("ip").setText(l.getIp() == null ? "" : l.getIp());
		el.addElement("expiration").setText(
				l.getExpiration() == null ? "" : formatter.format(l
						.getExpiration()));
		el.addElement("code").setText(l.getCode());
		el.addElement("signature").setText(l.getSignature());
		return doc;
	}

	public static L fromXML(String path) throws Exception {
		Document doc = XMLUtil.parseByPath(path);
		Element el = doc.getRootElement();
		String id = el.elementText("id");
		String product = el.elementText("product");
		String version = el.elementText("version");
		String code = el.elementText("code");
		L l = new L(id, product, version, code);
		String ip = el.elementText("ip");
		if (!Checker.isEmpty(ip))
			l.setIp(ip);
		String eDate = el.elementText("expiration");
		if (!Checker.isEmpty(eDate)) {
			Date expiresDate = formatter.parse(eDate);
			l.setExpiration(expiresDate);
		}
		l.setSignature(el.elementText("signature"));
		return l;
	}

}
